//
//  XMMainTabBar.swift
//  XMWeibo-Swift
//
//  Created by 育鑫 王 on 15/10/27.
//  Copyright © 2015年 育鑫 王. All rights reserved.
//

import UIKit

class XMMainTabBar: UITabBar {

    private let count : CGFloat = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算宽度
        let width = bounds.width / count
        
        //设置frame
        let frame = CGRect(x: 0, y: 0, width: width, height: bounds.height)
        
        var index = 0
        for view in subviews{
            if view is UIControl {
                print("view :\(view)")
                
//                view.frame = UIOffset(horizontal: (width * CGFloat(index)), vertical: 0)
                view.frame = CGRectOffset(frame, width * CGFloat(index), 0)
                
                index += index == 1 ? 2 : 1
                
            }
        }
        addButton.frame = CGRect(x: width * 2, y: 0, width: width, height: bounds.height)
        print("============")
    }
    
    //添加中间按钮
    lazy var addButton : UIButton = {
        let button = UIButton()
        
        // 按钮图片
        button.setImage(UIImage(named:"tabbar_compose_icon_add" ), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted" ), forState: UIControlState.Highlighted)
        
        //按钮背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        self.addSubview(button)
        
        return button
    }()
    
    
}
